<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceGetEventDetail extends core {

    public function process() {
        $event_id = $this->db->escape(trim($_POST['event_id']));
        $user_id = intval($_POST['user_id']);
        $data = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND `user_id`='$user_id'");
        $html = '';
        if ($data) {
            $total_guest = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$data->event_id");
            if(($data->start_time!="0000-00-00 00:00:00") && ($data->end_time!="0000-00-00 00:00:00")){
                if ($data->end_time == $data->start_date) {
                    $time = date('h:i A', strtotime($data->start_time));
                } else {
                    $time = date('h:i A', strtotime($data->start_time)) . ' - ' . date('h:i A', strtotime($data->end_time));
                }
            }else{
                $time='';
            }
            $html .= ' <div class="wd-list-event-item-detail">
                            <div class="wd-list-event-item-right">
                                <span class="wd-list-event-item-txt-title">' . date('m/d/Y', strtotime($data->start_date)) . '</span>
                                <span class="wd-list-event-item-txt-des">' . $time . '</span>
                                <div class="wd-list-event-item-left-grp-action">
                                    <span class="wd-list-event-act-add" rel="'.$data->event_id.'">
                                        <img src="img/add-icon-act.png">
                                        <span class="wd-list-event-act-txt" >Add Guest</span>
                                    </span>';
            if($total_guest->total>0){
                                    $html.='<span class="wd-list-event-act-adress btn-event-act-adress"  rel="'.$data->event_id.'">
                                        <img src="img/requestadress-icon-act.png">
                                        <span class="wd-list-event-act-txt">Request Address</span>
                                    </span>';
                                     $html .= '<span class="wd-list-event-act-adress wd-list-event-act-email"  rel="' . $data->event_id . '">
                                            <img src="img/email-ico-color.png">
                                            <span class="wd-list-event-act-txt">Email Guest List</span>
                                        </span>';
                                      if ($data->start_time == "0000-00-00 00:00:00") {
                   
                                        $html.='<span class="wd-list-event-act-adress wd-list-event-act-invite" rev="'.$data->title.'" rel="' . $data->event_id . '">
                                            <img src="img/invite-icon.png">
                                            <span class="wd-list-event-act-txt">Invite Guests </span>
                                        </span>';
                                      }else{
                                          $html.='<span class="wd-list-event-act-adress wd-list-event-act-invite"  rel="' . $data->event_id . '">
                                            <img src="img/invite-icon.png">
                                            <span class="wd-list-event-act-txt">Invite Guests </span>
                                        </span>';
                                      }
            }
             $html.='                   </div>
                                <div class="wd-list-event-item-right-grp-action">
                                    <a href="editEvent.html" class="wd-edit-guest" rel="'.$data->event_id.'"><img src="img/edit-icon.png"></a>';
                                    if($data->canbedeleted==1){
                                        $html.='<span class="wd-delete-guest" rel="' . $data->event_id . '"><img src="img/delete-icon.png"></span>';
                                        }
                                    $html.='</div>
                                    
                            </div>
                            <div class="wd-list-event-item-left">
                                <a href="eventDetail.html" class="wd-list-event-item-txt-title">' . $data->title . '</a>
                                <span class="wd-list-event-item-txt-des">' . $data->location . '</span>
                                <span class="wd-list-event-item-txt-des">' . nl2br($data->description) . '</span>
                            </div>
                        </div>';
                        
                       
                       
            $total_guest_maybe = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='0'");
            $total_guest_going = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='1'");
            $total_guest_not_going = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='2'");
            //going
            if($total_guest_going->total > 0){
                $data_guest_going = $this->db->get_results("SELECT g.firstname, g.lastname, g.photo FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 1 ORDER BY g.firstname asc LIMIT 0,2;");
                $html.='<div class="wd-event-status" id="guest_list_going">
                            <span class="wd-event-status-title">Going ('.$total_guest_going->total.')</span>
                           ';
                foreach ($data_guest_going as $guest_going) {
                    if($guest_going->photo !=""){
                        $photo_src = '<img src="'.$guest_going->photo.'" class="wd-grp-member-event-ava"/>';
                    }else{
                        $photo_src = '<img src="img/no-ava.png" class="wd-grp-member-event-ava"/>';                        
                    }
                    $html.='
                            <div class="wd-grp-member-event">
                                '.$photo_src.'
                                <div class="wd-grp-member-event-name">'.$guest_going->firstname.' '.$guest_going->lastname.'</div>
                            </div>
                            
                            ';
                }
                if($total_guest_going->total > 2){
                    $html.='
                           <div class="wd-more-line" rel="1">Show more</div>
                           <input type="hidden" name="page" id="page_going" value="1"/>
                           <input type="hidden" name="end_paging" id="end_paging_going" value="0"/>
                        ';
                }
                $html.='</div>';
            }else{
                $html.='<div class="wd-event-status" style="margin-bottom:10px;">
                            <span class="wd-event-status-title">Going </span>
                            
                        </div>';
            }
            //maybe
           
             if($total_guest_maybe->total > 0){
                $data_guest_maybe = $this->db->get_results("SELECT g.firstname, g.lastname, g.photo FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 0 ORDER BY g.firstname asc LIMIT 0,2;");
                $html.='<div class="wd-event-status" id="guest_list_maybe">
                            <span class="wd-event-status-title">Maybe ('.$total_guest_maybe->total.')</span>
                           ';
                
                foreach ($data_guest_maybe as $guest_maybe) {
                    if($guest_maybe->photo !=""){
                        $photo_src = '<img src="'.$guest_maybe->photo.'" class="wd-grp-member-event-ava"/>';
                    }else{
                        $photo_src = '<img src="img/no-ava.png" class="wd-grp-member-event-ava"/>';                        
                    }
                    $html.='
                            <div class="wd-grp-member-event">
                                '.$photo_src.'
                                <div class="wd-grp-member-event-name">'.$guest_maybe->firstname.' '.$guest_maybe->lastname.'</div>
                            </div>
                            
                            ';
                   
                }
                if($total_guest_maybe->total > 2){
                    $html.='
                            <div class="wd-more-line" rel="0">Show more</div>
                            <input type="hidden" name="page" id="page_maybe" value="1"/>
                            <input type="hidden" name="end_paging" id="end_paging_maybe" value="0"/>
                        ';
                }
                $html.='</div>';
            }else{
                $html.='<div class="wd-event-status" style="margin-bottom:10px;">
                            <span class="wd-event-status-title">Maybe </span>
                        </div>';
            }
            //not going
             if($total_guest_not_going->total > 0){
                $data_guest_not_going = $this->db->get_results("SELECT g.firstname, g.lastname, g.photo FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 2 ORDER BY g.firstname asc LIMIT 0,2;");
                $html.='<div class="wd-event-status" id="guest_list_not_going">
                            <span class="wd-event-status-title">Not Going ('.$total_guest_not_going->total.')</span>
                           ';
                foreach ($data_guest_not_going as $guest_not_going) {
                    if($guest_not_going->photo !=""){
                        $photo_src = '<img src="'.$guest_not_going->photo.'" class="wd-grp-member-event-ava"/>';
                    }else{
                        $photo_src = '<img src="img/no-ava.png" class="wd-grp-member-event-ava"/>';                        
                    }
                    $html.='
                            <div class="wd-grp-member-event">
                                '.$photo_src.'
                                <div class="wd-grp-member-event-name">'.$guest_not_going->firstname.' '.$guest_not_going->lastname.'</div>
                            </div>
                            
                            ';
                }
                if($total_guest_not_going->total > 2){
                    $html.='
                            <div class="wd-more-line" rel="2">Show more</div>
                            <input type="hidden" name="page" id="page_not_going" value="1"/>
                            <input type="hidden" name="end_paging" id="end_paging_not_going" value="0"/>
                        ';
                }
                $html.='</div>';
            }else{
                $html.='<div class="wd-event-status" style="margin-bottom:10px;">
                            <span class="wd-event-status-title">Not Going </span>
                        </div>';
            }
            $html .=' <div class="slide-page wd-fix">

                        </div>';
            
            $this->_JSONResponse(array('error' => 0, 'html' => $html));
        } else {
            $this->_JSONResponse(array('error' => 1,'msg'=> "The event does not exist! Please try again."));
        }
    }

}

$processor = new webServiceGetEventDetail();
$processor->process();
?>